﻿#include "stdafx.h"

#include "svgcolorselector.h"

#include <QColorDialog>
#include <QLabel>
#include <QPushButton>

static const QString CANVAS_COLOR_BTN_QSS("background-color:%1; border: 0px;");

SvgColorSelector::SvgColorSelector(QWidget *parent)
    : QWidget(parent), m_button(new QPushButton(this))
{
    setFixedSize(75, 75);
    const auto label = new QLabel(tr("Color:"), this);
    label->setGeometry(2, 0, 75, 20);

    m_button->setGeometry(0, 20, 75, 55);
    m_button->setFlat(true);
    m_button->setAutoFillBackground(true);

    connect(m_button, &QPushButton::clicked, this, &SvgColorSelector::_SelectColor);
}

void SvgColorSelector::SetColor(const QColor &color)
{
    m_button->setStyleSheet(CANVAS_COLOR_BTN_QSS.arg(color.name(QColor::HexRgb)));
}

void SvgColorSelector::_SelectColor()
{
    const QColor color = QColorDialog::getColor(m_button->palette().button().color());
    if (color.isValid())
    {
        SetColor(color);
        emit ColorSelected(color);
    }
}